package jobhunter.service;

import jobhunter.model.Company;
import jobhunter.model.Process;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ProcessRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProcessService
{
	@Autowired
	private CompanyRepository companyRepository;
	@Autowired
	private ProcessRepository processRepository;

	public void update(Long id, String name, String description, Long companyId)
	{
		Process process = processRepository.findById(id);
		process.setName(name);
		process.setDescription(description);
		process.setCompany( companyRepository.findById(companyId));
		processRepository.merge(process);		
	}

	public void add(String name, String description, Long companyId)
	{

		Process process = new Process(name,description);
		process.setCompany(companyRepository.findById(companyId));
		processRepository.persist(process);
	}

	public Process getLastProcessWithActionsByCompany(Company company)
	{
		Process process = processRepository.findLastOfCompany(company);
		if(process!=null)
			return processRepository.findByIdWithActions(process.getId());
		else 
			return null;
	}
}
